cd "C:\Users\au446231\OneDrive - Aarhus Universitet\RR Collapse\Replication"
//required packages: reghdfe and coefplot 
graph set window fontface "Garamond"

** Motivating fig.**
********************
use "across.dta",replace

*Figure 1*
bys HRE year: egen mean=mean(commune)
bys HRE year: egen mean2=mean(lnpop)
g m1=mean if HRE==1
g m2=mean if HRE==0
g mm1=mean2 if HRE==1
g mm2=mean2 if HRE==0
tw (scatter m1 year,color(black) msize(large)) (scatter m2 year,color(gray) msize(large)),ylabel(0 (0.2) 1) graphregion(color(white)) xline(1250,lcolor(gray) lpattern(dash)) xtitle(Year) xlabel(1100 (100) 1800) ytitle("Share of towns that are self-governing") title("") legend(order(2 "Non-imperial towns" 1 "Imperial towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))

graph export "intro.pdf",replace


**** Step one ******
********************
*Figure 3*
use "across.dta",replace
reghdfe commune pre constant post1-post6 ,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Baseline",color(black))
graph export "did.pdf",replace
eststo m2: reghdfe commune pre constant post1-post6 bis lnpop conf university capit domi c.dist_cl#i.year crusader#i.year,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Controls",color(black))
graph export "did_con.pdf",replace
eststo m3: reghdfe commune pre constant post1-post6 bis##after conf##after c.lnpop##after  crusader#i.year c.dist_cl#i.year after##domi university##after capit##after ,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Controls x Post",color(black))
graph export "did_con_post.pdf",replace

*Figure 4*
eststo ma: reghdfe commune pre##hohen constant post1##hohen post2##hohen post3##hohen post4##hohen post5##hohe post6##hohen,cluster(indicator) absorb(indicator year)
eststo m1:  margins,dydx(pre constant post*) at(hohen=(0)) noestimcheck post
eststo mb: reghdfe commune pre##hohen constant post1##hohen post2##hohen post3##hohen post4##hohen post5##hohe post6##hohen,cluster(indicator) absorb(indicator year)
eststo m2:  margins,dydx(pre constant post*) at(hohen=(1)) noestimcheck post
coefplot (m1, mcolor(black) ciopts(lcolor(black))) (m2,mcolor(gray) msymbol(circle_hollow) ciopts(lcolor(gray))), xtitle("Centuries relative to interregnum") vertical graphregion(color(white)) yline(0, lcolor(gray) lpattern(dash)) omitted xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ytitle("Change in share self-governing") xline(2,lcolor(black)) ylabel(-0.2 (0.2) 1) legend(order(2 "Other imperial towns" 4 "Hohenstaufen towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
graph export "tripledid.pdf",replace





**** Step two ******
********************
use "within.dta",replace
*Figure 7*
capture bys decade: egen ms=mean(selfgov)
capture bys decade: egen mh=mean(sygehus)
capture bys decade: egen msk=mean(skole)
capture bys decade: egen mm=mean(mur)
capture bys decade emperor: egen t=mean(institu)
capture g t1=t if emper==1
capture g t2=t if emper==0 
tw (scatter ms decade, mcolor(black) msymbol(circle_hollow)) (scatter mm decade, mcolor(gray) msymbol(circle_hollow)) (connected mh decade,lcolor(black) mcolor(black)) (connected msk decade, lcolor(gray) mcolor(gray)) if decade>1090,ylabel(0 (0.2) 1.1) xtitle("Decade") ytitle("") legend(order(1 "Share of towns with autonomy" 2 "Share of towns with defensive wall" 3 "Share of towns with hospital" 4 "Share of towns with school") region(lstyle(none) lcolor(white)) size(*0.95) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(2)) graphregion(color(white))
graph export "outcome.pdf",replace

*Figure 8*
tw (scatter t1 decade, mcolor(black) msymbol(triangle_hollow)) (scatter t2 decade, mcolor(gray)) if decade>1140 & decade<1360, xline(1250, lcolor(black)) ytitle("Avg. number of town councils, hospitals, schools, and walls", size(medsmall)) xtitle("Decade") graphregion(color(white)) legend(order(2 "Other imperial towns" 1 "Hohenstaufen towns") region(lstyle(none) lcolor(white)) size(*1) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
graph export "within.pdf",replace

*Figure 9*
eststo m1: qui reghdfe institutions event2-event25 constant event27-event42,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.4 (0.2) 1) ytitle("Change in institutions") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Baseline",color(black))
graph export "did2.pdf",replace
eststo m2: qui reghdfe institutions event2-event25 constant event27-event42 duni indu war ldist anym lending borg dominican crusader#i.decade rhine#i.decade c.dist_cluniac#i.decade,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.4 (0.2) 1) ytitle("Change in institutions") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Controls",color(black))
graph export "did2con.pdf",replace
eststo m3: qui reghdfe institutions event2-event25 constant event27-event42 indu##after war##after c.duni##after crusader#i.decade ldist##after anym##after lending##after borg##after dominican##after rhine#i.decade c.dist_cluniac#i.decade,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.4 (0.2) 1) ytitle("Change in institutions") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Controls x Post",color(black))
graph export "did2con_post.pdf",replace





	
**** Step three ****
********************
*Figure 10*
use "upop_agg.dta",replace
tw (connected upop_GE year, lcolor(black) mcolor(black)) (connected upop_France year,lcolor(gray) mcolor(gray)) (connected upop_weurope year,lcolor(black) msymbol(triangle_hollow) mcolor(black) lpattern(dash)) if year<1300 & year>900, xlabel(1000 1100 1200) xtitle(Century) graphregion(color(white)) ytitle(Total urban population) legend(order(1 "German Empire" 2 "France" 3 "W. Europe (excluding GE)")) ylabel(200 (200) 1400) legend(order(1 "German Empire" 2 "France" 3 "W. Europe (excluding GE)") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(2))
graph export "urban_des.pdf",replace

*Figure 11*
use "across.dta",replace
g p=0
recode p (0=1) if citypop_le5>0 & year==1200
bys indicator: egen POP12=max(p)
g cat1=0
recode cat1 (0=1) if POP12>0 & HRE==0
recode cat1 (0=2) if POP12==0 & HRE==1
recode cat1 (0=3) if POP12>0 & HRE==1
bys year cat1: egen avg=mean(commune)
g t1=avg if cat1==0
g t2=avg if cat1==1
g t3=avg if cat1==2
g t4=avg if cat1==3
capture graph drop *
tw (scatter t1 year,mcolor(black)) (scatter t2 year,mcolor(black) msymbol(circle_hollow))  if year>1000 & year<1600,name(a) ylabel(0 (0.2) 1) title("Outside Empire",color(black)) graphregion(color(white)) xline(1250,lcolor(black)) xtitle(Century) ytitle(Share of towns with self-government)  legend(order(1 "Small towns" 2 "Large towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
tw (scatter t3 year,mcolor(black)) (scatter t4 year,mcolor(black) msymbol(circle_hollow)) if year>1000 & year<1600,name(b) title("Within Empire",color(black)) ylabel(0 (0.2) 1) graphregion(color(white)) xline(1250,lcolor(black)) xtitle(Century) ytitle(Share of towns with self-government) legend(order(1 "Small towns" 2 "Large towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
graph combine a b, graphregion(color(white))
graph export "urban_ols.pdf",replace

*Figure 12*
use "primogeniture.dta", replace
tw (area share year, color(gray) lwidth(none))  if year<1301 & year>1099,graphregion(color(white)) xtitle(Year) ylabel(0.3 (0.2) 0.9) ytitle(Share of states with primogeniture)
graph export "primo.pdf",replace

*Figure 13*
use "across.dta",replace
g cat1=0
recode cat1 (0=1) if bishop==1 & HRE==0
recode cat1 (0=2) if bishop==0 & HRE==1
recode cat1 (0=3) if bishop==1 & HRE==1
bys year cat1: egen avg=mean(commune)
g t1=avg if cat1==0
g t2=avg if cat1==1
g t3=avg if cat1==2
g t4=avg if cat1==3
capture graph drop *
tw (scatter t1 year,mcolor(black)) (scatter t2 year,mcolor(black) msymbol(circle_hollow))  if year>1000 & year<1600,name(a) ylabel(0 (0.2) 0.8) title("Outside Empire",color(black)) graphregion(color(white)) xline(1250,lcolor(black)) xtitle(Century) ytitle(Share of towns with self-government)    legend(order(1 "Lay towns" 2 "Bishop towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
tw (scatter t3 year,mcolor(black)) (scatter t4 year,mcolor(black) msymbol(circle_hollow)) if year>1000 & year<1600,name(b) title("Within Empire",color(black)) ylabel(0 (0.2) 0.8) graphregion(color(white)) xline(1250,lcolor(black)) xtitle(Century) ytitle(Share of towns with self-government) legend(order(1 "Lay towns" 2 "Bishop towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
graph combine a b, graphregion(color(white))
graph export "reform.pdf",replace	
	
	
***Appendix***
**************

*Descriptive statistics*
//europe data
use "across.dta",replace
qui reghdfe commune pre constant post1-post6 bis lnpop plundered university capit domi c.dist_cl#i.year crusader#i.year,cluster(indicator) absorb(indicator year)
sum commune HRE hohen bis lnpop plund universi capit domi dist_clu crusader if e(sample)==1
//within data
use "within.dta",replace
qui reghdfe institutions event2-event25 event27-event42 indu ldist anym lending borg dominican crusader#i.decade rhine#i.decade c.dist_cluniac#i.decade,cluster(cityid) absorb(cityid decade)
sum institutions selfgov  sygehus skole mur empe indu ldist anym war lending borg duni dominican rhine dist_clun domini crusader if e(sample)==1

*Alternative map of Empire (C.1)*
use "across.dta",replace
eststo m1: reghdfe commune alt_event2 constant alt_event4-alt_event9 lnpop plundered university capit domi c.dist_cl#i.year crusader#i.year,cluster(indicator) absorb(indicator year)
qui margins,dydx(alt_event2 constant alt_event4-alt_event9) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("",color(black))
graph export "alt_x_did.pdf",replace

*Figure 4 with controls (C.2)*
use "across.dta",replace
eststo mc: reghdfe commune pre##hohen constant post1##hohen post2##hohen post3##hohen post4##hohen post5##hohe post6##hohen bis##after conf##after c.lnpop##after c.plundered##after crusader#i.year c.dist_cl#i.year after##domi university##after capit##after ,cluster(indicator) absorb(indicator year)
eststo m1:  margins,dydx(pre constant post*) at(hohen=(0)) noestimcheck post
eststo md: reghdfe commune pre##hohen constant post1##hohen post2##hohen post3##hohen post4##hohen post5##hohe post6##hohen bis##after conf##after c.lnpop##after c.plundered##after crusader#i.year c.dist_cl#i.year after##domi university##after capit##after ,cluster(indicator) absorb(indicator year)
eststo m2:  margins,dydx(pre constant post*) at(hohen=(1)) noestimcheck post
coefplot (m1, mcolor(black) ciopts(lcolor(black))) (m2,mcolor(gray) msymbol(circle_hollow) ciopts(lcolor(gray))), xtitle("Centuries relative to interregnum") vertical graphregion(color(white)) yline(0, lcolor(gray) lpattern(dash)) omitted xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ytitle("Change in share self-governing") xline(2,lcolor(black)) ylabel(-0.2 (0.2) 1) legend(order(2 "Other imperial towns" 4 "Hohenstaufen towns") region(lstyle(none) lcolor(white)) size(*1.2) symxsize(*.6) symysize(*.6) forcesize ring(0) position(11) colgap(tiny) row(1))
graph export "tripledid_con.pdf",replace


*Controlling for spatial dependence in self-government (C.3)*
use "across.dta",replace
eststo m1: reghdfe commune pre constant post1-post6 c.dsg12#after c.dsgt#after,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Baseline",color(black))
graph export "f1spa.pdf",replace
eststo m2: reghdfe commune pre constant post1-post6 bis c.dsg12#after c.dsgt#after lnpop conf university capit domi c.dist_cl#i.year crusader#i.year,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Controls",color(black))
graph export "f2spa.pdf",replace
eststo m3: reghdfe commune pre constant post1-post6 c.dsg12#after c.dsgt#after bis##after c.lnpop##after c.conf##after crusader#i.year c.dist_cl#i.year after##domi university##after capit##after ,cluster(indicator) absorb(indicator year)
qui margins,dydx(pre constant post*) 
marginsplot,graphregion(color(white)) xtitle("Centuries relative to interregnum") xlabel(1 "-2" 2 "-1" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5") ylabel(-0.1 (0.1) 0.4) ytitle("Change in share self-governing") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(2,lcolor(black)) title("Controls x Post",color(black))
graph export "f3spa.pdf",replace


*Within analyses split by outcome (C.4)*
use "within.dta",replace
eststo m1: qui reghdfe selfg event2-event25 constant event27-event42 ,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.2 (0.2) 0.4) ytitle("Change in share with institution") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Self-government",color(black))
graph export "y_sg.pdf",replace
eststo m2: qui reghdfe skole event2-event25 constant event27-event42 ,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.2 (0.2) 0.4) ytitle("Change in share with institution") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("School",color(black))
graph export "y_skole.pdf",replace
eststo m3: qui reghdfe sygehus event2-event25 constant event27-event42 ,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.2 (0.2) 0.4) ytitle("Change in share with institution") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Hospital",color(black))
graph export "y_syge.pdf",replace
eststo m4: qui reghdfe mur event2-event25 constant event27-event42 ,cluster(cityid) absorb(cityid decade)
qui margins, dydx(event19-event25 constant event27-event35) post noestimcheck
marginsplot,graphregion(color(white)) xtitle("Decades relative to interregnum") xlabel(1 "-8" 3 "-6"  5 "-4"  7 "-2"  9 "0" 11 "2" 13 "4" 15 "6" 17 "8" ) ylabel(-0.2 (0.2) 0.4) ytitle("Change in share with institution") title("") recast(scatter) ciopts(lcolor(black)) plotopts(mcolor(black)) yline(0,lcolor(gray) lpattern(dash)) xline(8,lcolor(black)) title("Defensive wall",color(black))
graph export "y_mur.pdf",replace

**The collapse and warfare (C.5)**
use "grid.dta",replace
capture graph drop *
reg conf empire##i.year,cluster(id)
margins, dydx(empire) at(year=(850 (50) 1750) ) noestimcheck
marginsplot,name(a)  graphregion(color(white)) xlabel(850 (150) 1750) xtitle(Period) ytitle(Coefficient on Empire) title("Baseline",color(black)) recast(scatter) plotopts(mcolor(black)) ciopts(lcolor(black)) xline(1250,lcolor(black)) yline(0, lcolor(gray) lpattern(dash))
reg conf empire##i.year c.bishop##after c.lnpop##after c.crusa##after c.domini##after c.uni##after c.clun##after c.capi##after,cluster(id) absorb(id)
margins, dydx(empire) at(year=(850 (50) 1750) ) noestimcheck
marginsplot,name(b)  graphregion(color(white)) xlabel(850 (150) 1750) xtitle(Period) ytitle(Coefficient on Empire) title("Controls",color(black)) recast(scatter) plotopts(mcolor(black)) ciopts(lcolor(black)) xline(1250,lcolor(black)) yline(0, lcolor(gray) lpattern(dash))
graph combine a b,graphregion(color(white))
graph export "war.pdf",replace



